library(ggthemr)
ggthemr('flat', spacing = 0.5, type = 'inner',layout="scientific")
anes2012$oldnew <- relevel(as.factor(anes2012$oldnew),ref="Old")
anes2012$oldnew2 <-  relevel(as.factor(anes2012$oldnew2),ref="Old")
a <- (lm(pk~efficacy1pre*mode*oldnew,anes2012))
summary(lm(pk~efficacy1pre*oldnew,subset(anes2012,mode<.5)))


b <- (lm(pk~efficacy2pre*mode*oldnew,anes2012))
c <- (lm(pk~efficacy3pre*mode*oldnew,anes2012))
d <- (lm(pk~efficacy4pre*mode*oldnew,anes2012))
e <- (lm(pk~pre_external*mode*oldnew,anes2012))
f <- (lm(pk~pre_internal*mode*oldnew,anes2012))

g <- (lm(pk~efficacy1po*mode*oldnew2,anes2012))
h <- (lm(pk~efficacy2po*mode*oldnew2,anes2012))
i <- (lm(pk~efficacy3po*mode*oldnew2,anes2012))
j <- (lm(pk~efficacy4po*mode*oldnew2,anes2012))
k <- (lm(pk~po_external*mode*oldnew2,anes2012))
l <- (lm(pk~po_internal*mode*oldnew2,anes2012))

names(a$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting political knowledge from three-way interactions between question type, efficacy, and mode",label = "tab:mode_pk",out = "Graphs/threewaymode_pk.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panela <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel A: DV is R's political knowledge")+geom_hline(y=0)




########################################
########################################
a <- (lm(pctchance~efficacy1pre*mode*oldnew,anes2012))
b <- (lm(pctchance~efficacy2pre*mode*oldnew,anes2012))
c <- (lm(pctchance~efficacy3pre*mode*oldnew,anes2012))
d <- (lm(pctchance~efficacy4pre*mode*oldnew,anes2012))
e <- (lm(pctchance~pre_external*mode*oldnew,anes2012))
f <- (lm(pctchance~pre_internal*mode*oldnew,anes2012))
summary(h)
g <- (lm(pctchance~efficacy1po*mode*oldnew2,anes2012))
h <- (lm(pctchance~efficacy2po*mode*oldnew2,anes2012))
i <- (lm(pctchance~efficacy3po*mode*oldnew2,anes2012))
j <- (lm(pctchance~efficacy4po*mode*oldnew2,anes2012))
k <- (lm(pctchance~po_external*mode*oldnew2,anes2012))
l <- (lm(pctchance~po_internal*mode*oldnew2,anes2012))
summary(lm(polactivity~efficacy3po*oldnew2,subset(anes2012,as.numeric(mode)==2)))

names(a$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting percent chance of voting from three-way interactions between question type, efficacy, and mode",label = "tab:mode_pctchace",out = "Graphs/threewayvotingmode.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)


threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelb <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel B: DV is R's Percent Chance of Voting")+geom_hline(y=0)

########################################
a <- (lm(polactivity~efficacy1pre*mode*oldnew,anes2012))
b <- (lm(polactivity~efficacy2pre*mode*oldnew,anes2012))
c <- (lm(polactivity~efficacy3pre*mode*oldnew,anes2012))
d <- (lm(polactivity~efficacy4pre*mode*oldnew,anes2012))
e <- (lm(polactivity~pre_external*mode*oldnew,anes2012))
f <- (lm(polactivity~pre_internal*mode*oldnew,anes2012))

g <- (lm(polactivity~efficacy1po*mode*oldnew2,anes2012))
h <- (lm(polactivity~efficacy2po*mode*oldnew2,anes2012))
i <- (lm(polactivity~efficacy3po*mode*oldnew2,anes2012))
j <- (lm(polactivity~efficacy4po*mode*oldnew2,anes2012))
k <- (lm(polactivity~po_external*mode*oldnew2,anes2012))
l <- (lm(polactivity~po_internal*mode*oldnew2,anes2012))



names(a$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(b$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(c$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(d$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(e$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(f$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(g$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(h$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(i$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(j$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(k$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )
names(l$coefficients) <- c("Intercept","Efficacy","Mode","Form","Effic. x Mode","Effic. x Form","Mode x Form","Effic. x Mode x Form" )

library(stargazer)
stargazer(a,b,c,d,e,f,g,h,i,j,k,l,omit.stat =c("f","ser"),float.env = "sidewaystable",dep.var.caption = "Efficacy Measure",column.labels =   c("1","2","3","4","External","Internal","1","2","3","4","External","Internal"),model.numbers = F,no.space = T,dep.var.labels.include = F,title="Predicting political activism from three-way interactions between question type, efficacy, and mode",label = "tab:mode_activism",out = "Graphs/threewaymode_activism.tex",initial.zero = T,digits = 2,font.size = "scriptsize",intercept.top = T,intercept.bottom=F)

threeway1 <- data.frame(do.call("rbind",lapply(list(a,b,c,d,e,f,g,h,i,j,k,l),function(x)summary(x)$coefficients[8,1:2])))
threeway1$question <- c("Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index","Efficacy 1","Efficacy 2","Efficacy 3","Efficacy 4","External\n Index","Internal\n Index")
threeway1$wave <- c(rep("Wave 1",6),rep("Wave 2",6))
names(threeway1)[2] <- "se"
panelc <- ggplot(threeway1,aes(y=Estimate,x=question))+geom_errorbar(aes(ymax=Estimate+1.64*se,ymin=Estimate-1.64*se),height=0)+geom_point(size=2,colour="black")+facet_grid(~wave)+xlab("Question")+ylab("b coefficient")+ggtitle("Panel C: DV is R's political activity")+geom_hline(y=0)


pdf("threewayplot_mode.pdf",width=12,height=12)
multiplot(panela,panelb,panelc)
dev.off()


